package com.example.photographWatermark

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.example.photographWatermark.camera2.CameraActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener

class ScanResultHandler(private val result: MethodChannel.Result) : ActivityResultListener {

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
        Log.d("-==============","onActivityResult");
        var scanResult = ByteArray(0)
        when (resultCode) {
            Activity.RESULT_OK -> {
                scanResult = data?.getByteArrayExtra(CameraActivity.EXTRA_RESULT)
                        ?: scanResult
            }
            Activity.RESULT_CANCELED -> {
//                scanResult = Protos.ScanResult.newBuilder()
//                        .setType(Protos.ResultType.Cancelled)
//                        .build()
//                        .toByteArray()
            }
            else -> {
                val errorCode = data?.getStringExtra(CameraActivity.EXTRA_ERROR_CODE)
//                scanResult = Protos.ScanResult.newBuilder()
//                        .setType(Protos.ResultType.Error)
//                        .setFormat(Protos.BarcodeFormat.unknown)
//                        .setRawContent(errorCode)
//                        .build()
//                        .toByteArray()
            }
        }
        result.success(scanResult)

        return true
    }
}